const app = getApp()

Page({
  data: {
    items:[]
  },
  //事件处理函数
  onItemClick: function (event) {
    var targetUrl = "/pages/welfare/peridetail/peridetail";
    if (event.currentTarget.dataset.url != null)
      targetUrl = targetUrl + "?url=" + event.currentTarget.dataset.url;
    wx.navigateTo({
      url: targetUrl
    });
  },
  onReady: function(){
    wx.setNavigationBarTitle({
      title: '福利',
    });
  },
  onReachBottom: function () {
    let that = this;
    that.setData({
      hidden: false,
    });
    requestData(that, mCurrentPage + 1);
  },
  onLoad: function () {
    let that = this;
    requestData(that, mCurrentPage + 1);
  }
})
let mCurrentPage = 0;
/**
 * 请求数据
 * @param that Page的对象，用来setData更新数据
 * @param targetPage 请求的目标页码
 */
function requestData(that, targetPage) {
  wx.showToast({
    title: '加载中',
    icon: 'loading'
  });
  wx.request({
    url: 'https://gank.io/api/data/%E7%A6%8F%E5%88%A9/20/' + targetPage,
    header: {
      "Content-Type": "application/json"
    },
    success: function (res) {
      if (res == null ||
        res.data == null ||
        res.data.results == null ||
        res.data.results.length <= 0) {
        console.error("god bless you...");
        return;
      }
      let concatValue = that.data.items;
      let itemList = concatValue.concat(res.data.results);
      that.setData({
        items: itemList
      });
      mCurrentPage = targetPage;
      wx.hideToast();
    }
  });
}

